package com.zf.shuashua.advert;

import android.app.Activity;

import com.bytedance.sdk.openadsdk.TTAdNative;
import com.miui.zeus.mimo.sdk.RewardVideoAd;
import com.vise.xsnow.http.callback.ACallback;
import com.zf.shuashua.AdvertUtils;
import com.zf.shuashua.bean.AdvertReport;
import com.zf.shuashua.bean.BaseData;
import com.zf.shuashua.manager.ActionThirdReportManager;
import com.zf.shuashua.utils.Util;

import java.lang.ref.WeakReference;

/**
 * 激励视频展示
 * 米盟激励视频
 */
public class RewardAdvertMimoManager {

    private RewardVideoAd mttRewardVideoAd;
    private String mAdvertType = AdvertConstants.REWARD_TYPE_TASK;
    private String mCodeId = "32a15a52b44a2f3b8cc3ca1cac29e091";
    private String mReportID = "";

    private static RewardAdvertMimoManager instance;
    private WeakReference<Activity> weakReference;
    private boolean isComplete = false;

    private RewardAdvertMimoManager() {
    }

    public static RewardAdvertMimoManager getInstance() {
        if (null == instance) {
            synchronized (RewardAdvertMimoManager.class) {
                if (null == instance) {
                    instance = new RewardAdvertMimoManager();
                }
            }
        }
        return instance;
    }

    public void showAdvert(Activity activity, String advertType, String reportId, OnRewardListener listener) {
        this.weakReference = new WeakReference<>(activity);
        this.mAdvertType = advertType;
        this.mReportID = reportId;
        isComplete = false;
        mCodeId = getCodeId();
        mttRewardVideoAd = new RewardVideoAd();
        loadAd(listener);
    }

    private void showAdvert() {
        if (mttRewardVideoAd != null) {
            mttRewardVideoAd.showAd(weakReference.get(), mRewardVideoInteractionListener);
        }
        reportRequestSucc(1);
    }

    private void reportRequestStart(){
        ActionThirdReportManager.getInstance().reportRequestStart(6, 2, mCodeId, 1);
    }

    private void reportRequestSucc(int return_num) {
        ActionThirdReportManager.getInstance().reportRequestSucc(6, 2, mCodeId, return_num);
    }

    private void reportRequestFail(String error_code, String error_msg){
        ActionThirdReportManager.getInstance().reportRequestFail(6, 2, mCodeId, 1, error_code, error_msg);
    }

    private void reportRequestRend(String error_code, String error_msg){
        ActionThirdReportManager.getInstance().reportRequestRend(6, 2, mCodeId, error_code, error_msg);
    }

    private void dealClose(String planId, OnRewardListener listener){
        if(!isComplete){
            return;
        }
        AdReportManager.getInstance().reportAdvertAction(uuiThirdAdvert, planId, AdvertReport.EventType.VideoEnd, getReportType(), "6", new ACallback<BaseData>() {
            @Override
            public void onSuccess(BaseData data) {
                if (data != null && data.isStatus()) {
                    weakReference.get().runOnUiThread(() -> {
                        if (listener != null) {
                            listener.onAdClose();
                        }
                    });
                }
            }

            @Override
            public void onFail(int errCode, String errMsg) {

            }
        });
    }

    private String uuiThirdAdvert;
    private String planId;
    private OnRewardListener listener;

    private void loadAd(OnRewardListener listener) {
        uuiThirdAdvert = Util.getUUID32();
        planId = System.identityHashCode(mttRewardVideoAd) + "";
        this.listener = listener;

        mttRewardVideoAd.loadAd(mCodeId, new RewardVideoAd.RewardVideoLoadListener() {
            @Override
            public void onAdRequestSuccess() {

            }

            @Override
            public void onAdLoadSuccess() {
                weakReference.get().runOnUiThread(() -> showAdvert());
            }

            @Override
            public void onAdLoadFailed(int code, String message) {
                weakReference.get().runOnUiThread(() -> {
                    if (listener != null) {
                        listener.onVideoError();
                    }
                });
                reportRequestFail(code + "", message);
            }
        });

        reportRequestStart();
    }

    private RewardVideoAd.RewardVideoInteractionListener mRewardVideoInteractionListener
            = new RewardVideoAd.RewardVideoInteractionListener() {
        @Override
        public void onAdPresent() {
            AdReportManager.getInstance().reportAdvertAction(uuiThirdAdvert, planId, AdvertReport.EventType.VideoStart, getReportType(), "6");
        }

        @Override
        public void onAdClick() {
            if (listener != null) {
                listener.onAdClick();
            }
            AdReportManager.getInstance().reportAdvertAction(uuiThirdAdvert, planId, AdvertReport.EventType.VideoClick, getReportType(), "6");
        }

        @Override
        public void onAdDismissed() {
//            Log.i(TAG, "onAdDismissed");
//            mShowLandscape.setEnabled(false);
//            mShowPortrait.setEnabled(false);
            dealClose(planId, listener);
        }

        @Override
        public void onAdFailed(String message) {
            weakReference.get().runOnUiThread(() -> {
                if (listener != null) {
                    listener.onVideoError();
                }
            });
            reportRequestRend("10", "video error");
        }

        @Override
        public void onVideoStart() {
//            AdReportManager.getInstance().reportAdvertAction(uuiThirdAdvert, planId, AdvertReport.EventType.VideoStart, getReportType());
        }

        @Override
        public void onVideoPause() {
//            Log.i(TAG, "onVideoPause");
        }

        @Override
        public void onVideoComplete() {
            isComplete = true;
            if (listener != null) {
                listener.onVideoComplete();
            }
        }

        @Override
        public void onPicAdEnd() {
//            Log.i(TAG, "onPicAdEnd");
        }
    };

    private String getReportType() {
        return AdvertUtils.getReportType(mAdvertType, mReportID);
    }

    private String getCodeId() {
        return AdvertUtils.getXiaomiId();
    }

}
